<?php

namespace App\Http\Services;


use App\Http\Repositories\Daos\User\UserDao;
use App\Http\Repositories\Redis\UserRedis;
use App\Http\Requests\UserContext;
use App\Models\Users;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;


class UserCenterService extends BaseService
{
    protected $userDao;

    public function __construct(UserDao $userDao)
    {
        $this->userDao = $userDao;
    }

    public function userInfo(Request $request)
    {
        $userId = $request->userId;
        $field  = ['id', 'mobile', 'nickname',  'avatar', 'role_id'];
        return $this->userDao->getOne(['id' => $userId], [], $field);
    }

    public function updateUserInfo(UserContext $userContext)
    {
        $userId = $userContext->getId();
        $post   = request()->input();
        $user   = Users::where('id', $userId)->first();
        if (!empty($post['nickname'])) {
            $user->nickname = $post['nickname'];
        }
        if (!empty($post['real_name'])) {
            $user->real_name = $post['real_name'];
        }
        if (!empty($post['contact'])) {
            $user->contact = $post['contact'];
        }
        if (!empty($post['plot'])) {
            $user->plot = $post['plot'];
        }
        if (!empty($post['role_value'])) {
            $user->role_value = $post['role_value'];
        }
        if (!empty($post['avatar'])) {
            $user->avatar = $post['avatar'];
        }
        Log::info('更新用户' . $userId . '信息', $post);
        $user->save();
        return true;
    }
}
